﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Microsoft.AspNet.Identity;
using Prav.Contracts;
using Prav.Model;
using prav.Models;

namespace prav.Controllers
{
    [Export(typeof(IController))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LentaController : Controller
    {
        private readonly IDalFactory _dalFactory;

        [ImportingConstructor]
        public LentaController(IDalFactory dalFactory)
        {
            _dalFactory = dalFactory;
        }

        public ActionResult Index()
        {
            using (var dal = _dalFactory.Create())
            {
                var posts =
                    dal.Posts.Where(post => post.ParentId == null).Take(20).Include(post => post.Author).ToList();

                PostsLentaViewModel lenta = new PostsLentaViewModel
                {
                    Posts = posts,
                };
                return View(lenta);
            }
        }

        [Authorize]
        public ActionResult New(long? parentId)
        {
            NewPostViewModel viewModel = new NewPostViewModel { ParentId = parentId };

            if (Request.IsAjaxRequest())
            {
                return PartialView(viewModel);
            }
            return View(viewModel);
        }

        [HttpPost]
        [Authorize]
        public ActionResult New(NewPostViewModel newPost)
        {
            if (!ModelState.IsValid)
            {
                return View(newPost);
            }

            using (var dal = _dalFactory.Create())
            {
                var dtnow = DateTime.Now;
                Post post = new Post
                {
                    Header = newPost.Header,
                    Body = newPost.Body,
                    ParentId = newPost.ParentId,
                    AuthorUserId = User.Identity.GetUserId(),
                    CreateTime = dtnow,
                    LastEditTime = dtnow,
                    Approved = false,
                    AccessLevel = 0,
                };

                dal.Posts.Add(post);
                dal.SaveChanges();
            }
            if (newPost.ParentId != null)
            {
                return RedirectToAction("ShowPost" + "/" + newPost.ParentId);
            }

            return RedirectToAction("Index");
        }

        public ActionResult ShowPost(long? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }

            using (var dal = _dalFactory.Create())
            {
                Post post = dal.Posts.Include(p => p.Author).Include(p => p.Comments).SingleOrDefault(p => p.Id == id);
                ShowPostViewModel viewModel = new ShowPostViewModel
                {
                    Post = post,
                };
                return View(viewModel);
            }
        }
    }
}